/************************************************************************************/
/*  Program name CAPAPIMP_INV_I.sql                                                 */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER AX_AP_INVOICES_ARU1 DISABLE;
ALTER TRIGGER AX_AP_INVOICES_ARU2 DISABLE;
ALTER TRIGGER AX_AP_INVOICES_ARU3 DISABLE;
ALTER TRIGGER AX_AP_INVOICES_ARU4 DISABLE;
ALTER TRIGGER AX_AP_INVOICES_BRDI1 DISABLE;
ALTER TRIGGER IGI_EXP_HOLD_T1 DISABLE;
ALTER TRIGGER IGI_SIA_AP_INVOICES_ALL_T1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_INV_I';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM CRST_AP_INVOICES;

	lv_Next_Seq_ID	NUMBER;
	lv_SOB_Name		VARCHAR2(20);
	lv_COA_ID		NUMBER;
	lv_Vend_ID		NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_INVOICES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_INVOICES_MAP VALUES
			(
			C1.INVOICE_ID,
			C1.VENDOR_ID,
			C1.INVOICE_NUM,
			C1.ORG_ID,
			C1.DOC_SEQUENCE_ID,
			C1.DOC_SEQUENCE_VALUE,
			lv_Next_Seq_ID,
			C1.VENDOR_ID,
			C1.INVOICE_NUM,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),
			C1.DOC_SEQUENCE_ID,		--Null
			C1.DOC_SEQUENCE_VALUE	--Null
			);

			--lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortName(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortNameOU(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(lv_SOB_Name);
			lv_Vend_ID	:=	CAPBRIMP_COMMON.GetVendorID_Name(C1.VENDOR_NAME);

			INSERT INTO AP_INVOICES_ALL
			VALUES
			(
			lv_Next_Seq_ID,											--INVOICE_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			lv_Vend_ID,												--VENDOR_ID,
			C1.INVOICE_NUM,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),			--SET_OF_BOOKS_ID,
			C1.INVOICE_CURRENCY_CODE,
			C1.PAYMENT_CURRENCY_CODE,
			C1.PAYMENT_CROSS_RATE,
			C1.INVOICE_AMOUNT,
			CAPBRIMP_COMMON.GetVendorSiteID(lv_Vend_ID,CAPBRIMP_MAPPING.GetOrgMapping(C1.ORG_ID),C1.VENDOR_SITE_CODE),	--VENDOR_SITE_ID,
			C1.AMOUNT_PAID,
			C1.DISCOUNT_AMOUNT_TAKEN,
			C1.INVOICE_DATE,
			C1.SOURCE,
			C1.INVOICE_TYPE_LOOKUP_CODE,
			REPLACE(C1.DESCRIPTION,'^C10^',CHR(10)),
			CAPBRIMP_MAPPING.GetAPBatchIDMapping(C1.BATCH_ID),		--BATCH_ID,
			C1.AMOUNT_APPLICABLE_TO_DISCOUNT,
			C1.TAX_AMOUNT,
			CAPBRIMP_COMMON.GetAPTerm(C1.AP_Term),					--TERMS_ID,
			C1.TERMS_DATE,
			C1.PAYMENT_METHOD_LOOKUP_CODE,
			C1.PAY_GROUP_LOOKUP_CODE,
			C1.ACCTS_PAY_CODE_COMBINATION_ID,
			C1.PAYMENT_STATUS_FLAG,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.BASE_AMOUNT,
			C1.VAT_CODE,
			C1.LAST_UPDATE_LOGIN,
			C1.EXCLUSIVE_PAYMENT_FLAG,
			C1.PO_HEADER_ID,				-- ???
			C1.FREIGHT_AMOUNT,
			C1.GOODS_RECEIVED_DATE,
			C1.INVOICE_RECEIVED_DATE,
			C1.VOUCHER_NUM,
			C1.APPROVED_AMOUNT,
			C1.RECURRING_PAYMENT_ID,		-- ???
			C1.EXCHANGE_RATE,
			C1.EXCHANGE_RATE_TYPE,
			C1.EXCHANGE_DATE,
			C1.EARLIEST_SETTLEMENT_DATE,
			C1.ORIGINAL_PREPAYMENT_AMOUNT,
			C1.DOC_SEQUENCE_ID,				-- ???
			C1.DOC_SEQUENCE_VALUE,
			C1.DOC_CATEGORY_CODE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY,
			C1.APPROVAL_STATUS,
			C1.APPROVAL_DESCRIPTION,
			C1.INVOICE_DISTRIBUTION_TOTAL,
			C1.POSTING_STATUS,
			C1.PREPAY_FLAG,
			CAPBRIMP_COMMON.GetUser(C1.AUTHORIZED_BY_NAME),			--AUTHORIZED_BY,
			C1.CANCELLED_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CANCELLED_BY_NAME),			--CANCELLED_BY,
			C1.CANCELLED_AMOUNT,
			C1.TEMP_CANCELLED_AMOUNT,
			C1.PROJECT_ACCOUNTING_CONTEXT,
			C1.USSGL_TRANSACTION_CODE,
			C1.USSGL_TRX_CODE_CONTEXT,
			C1.PROJECT_ID,
			C1.TASK_ID,
			C1.EXPENDITURE_TYPE,
			C1.EXPENDITURE_ITEM_DATE,
			C1.PA_QUANTITY,
			C1.EXPENDITURE_ORGANIZATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PA_DEFAULT_DIST_CCID_D),	--PA_DEFAULT_DIST_CCID,
			C1.VENDOR_PREPAY_AMOUNT,
			C1.PAYMENT_AMOUNT_TOTAL,
			C1.AWT_FLAG,
			C1.AWT_GROUP_ID,
			C1.REFERENCE_1,
			C1.REFERENCE_2,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.PRE_WITHHOLDING_AMOUNT,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.AUTO_TAX_CALC_FLAG,
			C1.PAYMENT_CROSS_RATE_TYPE,
			C1.PAYMENT_CROSS_RATE_DATE,
			C1.PAY_CURR_INVOICE_AMOUNT,
			C1.MRC_BASE_AMOUNT,
			C1.MRC_EXCHANGE_RATE,
			C1.MRC_EXCHANGE_RATE_TYPE,
			C1.MRC_EXCHANGE_DATE,
			C1.GL_DATE,
			C1.AWARD_ID,
			C1.PAID_ON_BEHALF_EMPLOYEE_ID,		-- ???
			C1.AMT_DUE_CCARD_COMPANY,
			C1.AMT_DUE_EMPLOYEE,
			C1.APPROVAL_READY_FLAG,
			C1.APPROVAL_ITERATION,
			C1.WFAPPROVAL_STATUS,
			CAPBRIMP_COMMON.GetUser(C1.REQUESTER_NAME)				--REQUESTER_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AX_AP_INVOICES_ARU1 ENABLE;
ALTER TRIGGER AX_AP_INVOICES_ARU2 ENABLE;
ALTER TRIGGER AX_AP_INVOICES_ARU3 ENABLE;
ALTER TRIGGER AX_AP_INVOICES_ARU4 ENABLE;
ALTER TRIGGER AX_AP_INVOICES_BRDI1 ENABLE;
ALTER TRIGGER IGI_EXP_HOLD_T1 ENABLE;
ALTER TRIGGER IGI_SIA_AP_INVOICES_ALL_T1 ENABLE;
